import { BasicDrawer, useDrawerInner } from "@/components/Drawer";
import { message } from "antd";
import { useTranslation } from "react-i18next";
import "./index.less";
import { drugDatch } from "@/api/MedicalExamination";
import { WarehouseApi } from "@/api/erp/stock/warehouse";
import { BasicProTable, useProTable } from "@/components/ViewComponents/proTable";
import { drugInfoApi } from "@/api/erp/drug/drugCatalogue";
import { drugFrmLossApi } from "@/api/erp/drug/reportingLosses";
let reload: Function;
let pkId: string;
const index = (props: { register: Fn }) => {
	const { t } = useTranslation();
	// const en_vi = "erp.invtDept";
	const defaultColumns = [
		{
			title: "药品",
			dataIndex: "drugName"
		},
		{
			title: "批次",
			dataIndex: "batchNo",
			editable: true,
			type: "ApiSelect",
			componentProps: {
				api: drugDatch,
				labelField: "batchNo",
				valueField: "batchNo",
				onChange: () => {
					onChangeProduct();
				}
			}
		},
		{
			title: "仓库",
			dataIndex: "whsId",
			editable: true,
			type: "ApiSelect",
			componentProps: {
				api: WarehouseApi.getWarehouseSimpleList,
				labelField: "name",
				valueField: "whsId",
				onChange: () => {
					onChangeProduct();
				}
			}
		},
		{
			title: "数量",
			dataIndex: "drugId"
		}
	];

	const [registerDrawer, { closeDrawer }] = useDrawerInner(props.register, async data => {
		reload = data.reload;
		pkId = data.record.pkId;
	});
	const [proTable, { getRowData, setRowData, submit }] = useProTable({
		defaultColumns,
		api: drugInfoApi.drugsList,
		key: "drugId",
		type: "Checkbox"
		// returnValue: {
		// 	drugId: "drugId"
		// }
	});
	const onChangeProduct = async () => {
		const row = getRowData();
		console.log(row);
		if (!row.batchNo || !row.whsId) return;
		row.productId = row.whsId;
		const res = await drugFrmLossApi.drugBatchGetCount(row);
		setRowData({
			drugId: res.data
		});
	};
	const handleSubmit = async () => {
		const tableValue = await submit();
		console.log(tableValue);
		return;
		pkId ? message.success(t("kevin_ui.updateSuccess")) : message.success(t("kevin_ui.addedSuccessfully"));
		reload();
		closeDrawer();
		pkId = "";
	};
	return (
		<BasicDrawer showFooter={true} size="large" {...props} register={registerDrawer} ok={handleSubmit}>
			<div className="warehouses">
				<BasicProTable register={proTable}></BasicProTable>
			</div>
		</BasicDrawer>
	);
};
export default index;
